// ==UserScript==
// @name         quiz capture
// @namespace    https://github.com/luyanfei
// @version      0.1
// @description  capture quiz
// @author       luyanfei
// @include      *://time.geekbang.org/quiz/*
// @connect      *.geekbang.org
// @grant        GM_addStyle
// @grant        GM_setValue
// @grant        GM_getValue
// @grant        GM_download
// @grant        GM_xmlhttpRequest
// @grant        GM_setClipboard
// @icon         
// ==/UserScript==
let questions = [];
function createButton(right, action) {
    var div = document.createElement('div');
    div.style.padding = '3px';
    div.style.margin = '3px';
    div.style.backgroundColor = '#eee';
    div.style.cursor = 'pointer';
    div.style.position = 'absolute';
    div.style.top = '100px';
    div.style.right = right;
    div.style.display = 'block';
    div.appendChild(document.createTextNode(action));
    document.body.appendChild(div);
    return div
}
(function() {
    let saveBtn = createButton('50px', '抓取')
    saveBtn.addEventListener('click', () => {
        let title = document.getElementsByClassName('quiz-title')[0]
        let item = {'title': title.textContent}
        let choices = document.getElementsByClassName('choice-question')[0]
        item.choices = Array.from(choices.getElementsByTagName('p'))
            .map(it => it.textContent)
        item.answer = document.getElementsByClassName('answer')[0].textContent
        console.log(JSON.stringify(item))
        questions.push(item)
    })
    let printBtn = createButton('150px', '拷贝')
    printBtn.addEventListener('click', () => {
        let content = JSON.stringify(questions)
        GM_setClipboard(content, 'text')
        console.log(content)
    })
})();
